package com.my.ddd_sample_collections.association_object.domain;

import com.my.ddd_sample_collections.association_object.south.adapter.repositories.UserRepositoryDB;
import com.my.ddd_sample_collections.association_object.south.adapter.repositories.dao.UserDAO;
import com.my.ddd_sample_collections.association_object.south.port.repositories.UserRepository;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertEquals;


/**
 * 测试 User， 依赖仓储， 但不依赖数据库（DB访问需要mock）
 */
public class UserTest_domain_object_and_repository_impl_but_mock_dao {

    @Test
    void test_getTotalSubscriptionFee() {
        //given
        UserDAO userDao = mock(UserDAO.class);
        when(userDao.findById(anyLong())).thenReturn(new User());
        when(userDao.calculateSubscriptonFee(anyLong())).thenReturn(222.0);

        UserRepository repo = new UserRepositoryDB(userDao);
        User user = repo.findById(1L);

        //when
        double fee = user.getTotalSubscriptionFee();

        //then
        assertEquals(222.0, fee);
        verify(userDao).findById(anyLong());
        verify(userDao).calculateSubscriptonFee(anyLong());

    }
}